package encrypt;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import db.BeanAssist;
import db.DbHelper;

import java.util.ArrayList;
import java.util.List;
/**
 * @author: wzq
 * @date: 14-6-17
 * description: change it at File | setting | File and code templates | include | file header
 */

public class DaoAbc {

    // 建表
    public static void createTable(Context ctx) {
        String sql = "create table if not exists tbl_abc (" +
                " age integer," +
                " name varchar(20)," +
                " password varchar(20)," +
                " stuff bolb"+
                ")";
        SQLiteDatabase db = new DbHelper(ctx).getWritableDatabase();
        db.execSQL(sql);
    }

    // 插入
    public static void insert(Context ctx, BeanAbc aAbc) {
        String sql = "insert into tbl_abc (age,name,password,stuff) values(?,?,?,?)";
        BeanAssist.encryptObj(aAbc);//TODO:注意这里
        System.out.println("-------begain insert ----- \n" + aAbc.toString());
        Object[] values = {aAbc.getAge(), aAbc.getName(), aAbc.getPassword(),aAbc.getStuff()};
        SQLiteDatabase db = new DbHelper(ctx).getWritableDatabase();
        db.execSQL(sql, values);
        try {
            db.close();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }


    // update
    public static void update(Context ctx, String oldPwd, String newPwd) {
        String sql = "update tbl_abc set password='%s' where password='%s'";
        sql = String.format(sql, newPwd, oldPwd);
        System.out.println("sql = " + sql);
        sql = BeanAssist.encryptSql(sql, BeanAbc.class);
        System.out.println("sql = " + sql);

        //
        SQLiteDatabase db = new DbHelper(ctx).getWritableDatabase();
        db.execSQL(sql);
        db.close();
    }


    // 按名字查询
    public static List<BeanAbc> queryByName(Context ctx, String name) {
        String sql = "select * from tbl_abc where name='%s' order by age asc";
        sql = String.format(sql, name);
        System.out.println("sql = " + sql);
        return query(ctx, sql);
    }

    // 查询
    public static List<BeanAbc> query(Context ctx, String sql) {
        List<BeanAbc> listAbc = new ArrayList<BeanAbc>();
        SQLiteDatabase db = new DbHelper(ctx).getWritableDatabase();
        Cursor cursor = db.rawQuery(sql, null);
        if(cursor != null && cursor.getColumnCount() > 0) {
            int indexName = cursor.getColumnIndex("age");
            int indexAge = cursor.getColumnIndex("name");
            int indexPwd = cursor.getColumnIndex("password");
            int indexStuff = cursor.getColumnIndex("stuff");
            cursor.moveToFirst();
            while(!cursor.isAfterLast()) {
                BeanAbc aAbc = new BeanAbc();
                aAbc.setAge(cursor.getInt(indexAge));
                aAbc.setName(cursor.getString(indexName));
                aAbc.setPassword(cursor.getString(indexPwd));
                aAbc.setStuff(cursor.getBlob(indexStuff));
                BeanAssist.decryptObj(aAbc);//TODO:注意这里
                listAbc.add(aAbc);
                cursor.moveToNext();
            }

            try {
                if(cursor != null) {
                    cursor.close();
                }
                db.close();
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
        return listAbc;
    }
}
